package com.xsl.culture.mybasevideoview.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.TextView;

import com.xapp.jjh.logtools.logger.LogUtil;
import com.xsl.culture.R;
import com.xsl.culture.mybasevideoview.base.LanBaseActivity;
import com.xsl.culture.mybasevideoview.controller.NetworkReq;
import com.xsl.culture.mybasevideoview.model.RequestCode;
import com.xsl.culture.mybasevideoview.utils.ScreenUtil;
import com.xsl.culture.mybasevideoview.utils.UiUtils;
import com.xsl.culture.mybasevideoview.utils.XslUtils;
import com.xsl.culture.mybasevideoview.view.secondui.DesActivity;

import java.lang.ref.WeakReference;
public class ChapterActivity extends LanBaseActivity {
    RecyclerView recyclerView;
    int selectedIndex = 0;
    int progress = 0;
    int current_time = 0;
    public static final String chapter_key = "chapter";
    public static final String chapter_content = "chapter_content";
    ChapterSeekBar main_controller_seek_bar;
    ChapterAdapter chapterAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_chapter);
        XslUtils.hideStausbar(new WeakReference<>(this), true);
        Intent intent = getIntent();
        selectedIndex = intent.getIntExtra(String.valueOf(R.string.activity_value), 1);
        progress = intent.getIntExtra("seekbar_current_progress", 1);
        current_time = intent.getIntExtra("current_time", 0);
        LogUtil.e("当前时间>>>>>"+current_time);
        init();
        findViewById(R.id.language).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toActivity(DesActivity.class);
            }
        });

        TextView time = findViewById(R.id.startTime);
        time.setText(XslUtils.convertSecToTimeString(current_time/1000));
    }

    private final String TAG = "Chapter";
    void init() {
        recyclerView = findViewById(R.id.chapter_recycle_view);
        main_controller_seek_bar = findViewById(R.id.main_controller_seek_bar);
        recyclerView.setHasFixedSize(true);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setOrientation(StaggeredGridLayoutManager.HORIZONTAL);
        recyclerView.setLayoutManager(layoutManager);
        //recyclerView.setAlpha(0.5f);
        chapterAdapter = new ChapterAdapter(selectedIndex);
        chapterAdapter.setOnItemClickListenner(new ChapterAdapter.OnItemClickListenner() {
            @Override
            public void onItemClick(View view, int position) {
                //Toast.makeText(getBaseContext(),"long click " + position + " item", Toast.LENGTH_SHORT).show();
                //view.setBackground(R.drawable.xsl_chapter_item_selected);
                chapterAdapter.setThisPostion(position);
                chapterAdapter.notifyDataSetChanged();
                //这里可以直接改变颜色，但是你不知道哪个需要变回去
                //view.setBackgroundResource(R.drawable.xsl_chapter_item_selected);
                selectedIndex = position+1;

//                int size = NetworkReq.getInstance().getChapterListInfo().getData().size();
//                if (selectedIndex % 2 == 0) {
//                    //左边的
//                    selectedIndex = selectedIndex / 2 + 1;
//                } else {
//                    selectedIndex = (size + 1) / 2 + (selectedIndex + 1) / 2;
//                }
                Log.d(TAG, "ret chapterIndex:" + selectedIndex);
                Intent intent = new Intent();
                intent.putExtra(chapter_key, selectedIndex);
                intent.putExtra(chapter_content,chapterAdapter.getPositionText(position));
                setResult(RequestCode.Chapter_req, intent);
                finish();
            }
        });
        recyclerView.setAdapter(chapterAdapter);

        findViewById(R.id.close_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int size = NetworkReq.getInstance().getChapterListInfo().getData().size();
                if (selectedIndex % 2 == 0) {
                    //左边的
                    selectedIndex = selectedIndex / 2 + 1;
                } else {
                    selectedIndex = (size + 1) / 2 + (selectedIndex + 1) / 2;
                }
                Log.d(TAG, "ret chapterIndex:" + selectedIndex);
                Intent intent = new Intent();
                intent.putExtra(chapter_key, -1);
                setResult(RequestCode.Chapter_req, intent);
                finish();
            }
        });

        main_controller_seek_bar.setChapterListInfo(NetworkReq.getInstance().getChapterListInfo(), 3*3600*1000+28*60*1000+15*1000);
        main_controller_seek_bar.invalidate();

        recyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int startX = (ScreenUtil.getScreenWidth(getBaseContext())-recyclerView.getWidth())/2+ScreenUtil.dp2px(50);
                main_controller_seek_bar.setLayoutFresh(startX);
            }
        });
        main_controller_seek_bar.setMax(1000);
        main_controller_seek_bar.setProgress(progress);
    }

    @Override
    protected void onResume() {
        super.onResume();
        UiUtils.hideBottomUIMenu(this);
    }

    @Override
    public void updateLanguage() {
        super.updateLanguage();
        chapterAdapter.getItemCount();
        chapterAdapter.notifyDataSetChanged();
    }
}
